namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util.BaseControls;
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Windows.Forms;

    internal class GridEditorComboBox : MaskedComboBox, IGridEditor
    {
        private static string _buf = string.Empty;
        internal CellStyle _cellStyle;
        internal int _col;
        internal Type _enumType;
        internal bool _fireEvents;
        internal SizeF _itemSize;
        private static DateTime _lastChar = DateTime.MinValue;
        internal int _row;
        internal C1FlexGridBase _view;
        internal bool _wasDown;

        internal GridEditorComboBox(C1FlexGridBase view)
        {
            this._row = this._col = -1;
            this._view = view;
            this._itemSize = new SizeF(0f, 0f);
            this._wasDown = false;
        }

        private bool AutoSearch(char chr)
        {
            return ((base.DropDownStyle == ComboBoxStyle.DropDownList) && this.AutoSearchList(chr));
        }

        private bool AutoSearchCombo(char chr)
        {
            if (chr < ' ')
            {
                return false;
            }
            string text = this.Text;
            int num = this.FindString(text);
            if (num < 0)
            {
                return false;
            }
            this.SelectedIndex = num;
            return true;
        }

        private bool AutoSearchList(char chr)
        {
            if (this.AutoSearchListBuffer(chr))
            {
                return true;
            }
            _buf = string.Empty;
            return false;
        }

        private bool AutoSearchListBuffer(char chr)
        {
            if (chr < ' ')
            {
                return false;
            }
            if (_buf.Length > 0)
            {
                TimeSpan span = (TimeSpan) (DateTime.Now - _lastChar);
                if (span.TotalSeconds > 0.5)
                {
                    _buf = string.Empty;
                }
            }
            _buf = _buf + chr;
            _lastChar = DateTime.Now;
            int num = -1;
            int selectedIndex = this.SelectedIndex;
            if ((_buf.Length > 1) && (selectedIndex > -1))
            {
                num = this.FindString(_buf, selectedIndex - 1);
            }
            if (((num < 0) && (selectedIndex > -1)) && (selectedIndex < (base.Items.Count - 1)))
            {
                num = this.FindString(_buf, selectedIndex);
            }
            if (num < 0)
            {
                num = this.FindString(_buf);
            }
            if (num < 0)
            {
                return false;
            }
            this.SelectedIndex = num;
            return true;
        }

        private int FindString(string s)
        {
            return this.FindString(s, 0);
        }

        private int FindString(string s, int start)
        {
            for (int i = start; i < base.Items.Count; i++)
            {
                if (string.Compare(base.Items[i].ToString(), s, true) == 0)
                {
                    return i;
                }
            }
            return -1;
        }

        public object GetContent()
        {
            return this.GetContent(this.SelectedIndex);
        }

        public object GetContent(int index)
        {
            if (index < 0)
            {
                return base.GetValidContent();
            }
            string str = base.Items[index].ToString();
            IDictionary cellDataMap = this._view._grid.GetCellDataMap(this._row, this._col);
            if (cellDataMap != null)
            {
                foreach (object obj2 in cellDataMap.Keys)
                {
                    if (cellDataMap[obj2].Equals(str))
                    {
                        return obj2;
                    }
                }
            }
            return str;
        }

        private void InitEditor(int row, int col)
        {
            this._row = row;
            this._col = col;
            this._view.ShowCell(row, col);
            base.DropDownStyle = ComboBoxStyle.DropDownList;
            this._wasDown = false;
            base.Items.Clear();
            string cellComboList = this._view._grid.GetCellComboList(row, col);
            if (((base.Items.Count == 0) && (cellComboList != null)) && (cellComboList.Length > 0))
            {
                if (cellComboList[0] == '|')
                {
                    base.DropDownStyle = ComboBoxStyle.DropDown;
                }
                foreach (string str2 in cellComboList.Split(new char[] { '|' }))
                {
                    if (str2.Length > 0)
                    {
                        base.Items.Add(str2);
                    }
                }
            }
            IDictionary cellDataMap = this._view._grid.GetCellDataMap(row, col);
            if ((base.Items.Count == 0) && (cellDataMap != null))
            {
                foreach (object obj2 in cellDataMap.Keys)
                {
                    base.Items.Add(cellDataMap[obj2].ToString());
                }
            }
            this._enumType = null;
            this._cellStyle = this._view.GetCellStyleDisplay(row, col);
            this._fireEvents = false;
            this.Font = this._cellStyle.Font;
            this.BackColor = this._cellStyle.BackColor;
            this.ForeColor = this._cellStyle.ForeColor;
            base.Mask = this._view._grid.GetCellEditMask(row, col);
            this.Text = string.Empty;
            this.UpdatePosition();
            this._view.OnSetupEditor(row, col);
        }

        protected void OnCloseUp()
        {
            this._view.OnComboCloseUp(this._row, this._col);
            this._wasDown = false;
            this._view.FinishEditing();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            e.Handled = this._view.OnKeyDownEdit(this._row, this._col, e);
            if (!e.Handled)
            {
                if ((e.KeyCode != Keys.Tab) && (e.KeyCode != Keys.Return))
                {
                    base.OnKeyDown(e);
                }
                if (!e.Handled)
                {
                    bool flag = false;
                    bool cancel = false;
                    bool flag3 = true;
                    switch (e.KeyCode)
                    {
                        case Keys.Prior:
                        case Keys.Next:
                        case Keys.Tab:
                            flag = true;
                            break;

                        case Keys.Left:
                            flag = !base.IsCombo();
                            break;

                        case Keys.Right:
                            flag = !base.IsCombo();
                            break;

                        case Keys.Escape:
                            flag = cancel = true;
                            flag3 = false;
                            break;

                        case Keys.Return:
                            if (!e.Shift && !e.Control)
                            {
                                flag = true;
                            }
                            break;
                    }
                    if (flag)
                    {
                        e.Handled = true;
                        this._view.OnComboCloseUp(this._row, this._col);
                        if (this._view.FinishEditing(cancel) && flag3)
                        {
                            this._view._kbdHandler.HandleEditorKey(e);
                        }
                    }
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (!e.Handled)
            {
                e.Handled = this._view.OnKeyPressEdit(this._row, this._col, e);
                if (!e.Handled)
                {
                    e.Handled = ((e.KeyChar == '\r') || (e.KeyChar == '\x001b')) || (e.KeyChar == '\t');
                    if (!e.Handled)
                    {
                        e.Handled = this.AutoSearch(e.KeyChar);
                    }
                }
            }
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            e.Handled = this._view.OnKeyUpEdit(this._row, this._col, e);
            if (!e.Handled)
            {
                base.OnKeyUp(e);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.OnCloseUp();
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (this._fireEvents && !base.IsUpdating())
            {
                this._fireEvents = false;
                this._view.OnChangeEdit();
                this._fireEvents = true;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (this._fireEvents && !base.IsUpdating())
            {
                this._fireEvents = false;
                this._view.OnChangeEdit();
                this._fireEvents = true;
            }
        }

        public void StartEditing(int row, int col, char key)
        {
            this.InitEditor(row, col);
            base.Visible = true;
            base.Focus();
            if (key <= ' ')
            {
                string dataDisplay = this._view._grid.GetDataDisplay(row, col);
                this.SelectedIndex = this.FindString(dataDisplay);
                if ((this.SelectedIndex < 0) && base.IsCombo())
                {
                    this.Text = this._view._grid.GetDataDisplay(row, col);
                }
            }
            else
            {
                this.SelectedIndex = this.FindString(key.ToString());
                if (base.IsCombo())
                {
                    int length = base.EditPosition(0);
                    if (this.SelectedIndex < 0)
                    {
                        this.Text = (this.Text.Length > 0) ? (this.Text.Substring(0, length) + key + this.Text.Substring(length + 1)) : key.ToString();
                        if ((length < this.Text.Length) && (char.ToLower(this.Text[length]) == char.ToLower(key)))
                        {
                            length = base.EditPosition(1);
                        }
                    }
                    else
                    {
                        length = base.EditPosition(1);
                    }
                }
                else
                {
                    this.AutoSearch(key);
                }
            }
            this._fireEvents = true;
            this._view.OnChangeEdit();
        }

        public void StartEditing(int row, int col, Point pt, bool dblClick)
        {
            this.InitEditor(row, col);
            if (this._view._grid.GetCellDataMap(row, col) != null)
            {
                object data = this._view._grid.GetData(row, col);
                if (data != null)
                {
                    this.SelectedIndex = base.Items.IndexOf(data);
                }
            }
            if (this.SelectedIndex < 0)
            {
                this.Text = this._view._grid.GetDataDisplay(row, col);
            }
            if (dblClick && (base.Items.Count > 0))
            {
                int selectedIndex = this.SelectedIndex;
                if ((selectedIndex > -1) || !base.IsCombo())
                {
                    this.SelectedIndex = (selectedIndex + 1) % base.Items.Count;
                    if (this._view.FinishEditing())
                    {
                        return;
                    }
                }
            }
            base.Visible = true;
            base.Focus();
            this._fireEvents = true;
            this._view.OnChangeEdit();
        }

        public void UpdatePosition()
        {
            Rectangle cellRectEditor = this._view.GetCellRectEditor(this._row, this._col);
            if ((cellRectEditor.Height < 6) || (cellRectEditor.Width < 6))
            {
                base.Bounds = new Rectangle(-100, -100, 10, 10);
            }
            else
            {
                cellRectEditor.Inflate(2, 2);
                base.Bounds = cellRectEditor;
            }
        }
    }
}

